<?php
/**
 * Author: 式神(vashi)
 * email: 289650682@qq.com
 * Date: 2019/1/10 18:24
 * FileName: NoAuth.php
 */

namespace app\index\controller;

//不验证权限,只验证登录
use app\common\model\Admin;
use app\common\model\Rule as Rls;
use luck\File;
use think\facade\Cache;
use think\facade\Env;
use think\Request;

class NoAuth extends Common
{
    protected $middleware = ['Auth'];
    //菜单
    public function getmenu(Request $request){
        $cate=Rls::where(['status'=>1,'showed'=>1])->order('sort','asc')->select();
        $cates=[];

        $uid=$request->userdata[config('app.ADMIN_AUTH_KEY')];
        if(in_array($uid,config('app.ADMINISTRATOR'))){
            $cates=$cate;
        }else{
            $user=Admin::field('password',true)->find($uid);
            $group=$user->roleGroup;
            $rid='';
            foreach($group as $g){
                $rid.=$g['rules'].',';
            }

            $rids=array_filter(array_unique(explode(',',$rid)));

            foreach($cate as $v){
                if(in_array($v['id'],$rids)){
                    $cates[]=$v;
                }
            }
        }
        $res=[
            "code"=>0,
            "data"=>rule_tree($cates)
        ];
        /*$res = [
            "data"=>[
                [
                    "id"=> 1,
                    "key"=> 10000,
                    "title"=> "权限管理",
                    "type"=> "user",
                    "url"=> "/auth/manager",
                    "children"=>  [
                        [
                            "id"=> 10,
                            "key"=>  20000,
                            "title"=>  "管理员管理",
                            "type"=>  "user",
                            "url"=>  "/auth/manager"
                        ],
                        [
                            "id"=>  11,
                            "key"=>  20011,
                            "title"=>  "规则管理",
                            "type"=>  "user",
                            "url"=>  "/auth/rule"
                        ],
                        [
                            "id"=>  12,
                            "key"=>  20012,
                            "title"=>  "角色管理",
                            "type"=>  "user",
                            "url"=>  "/auth/role"
                        ]
                    ]
                ],
                [
                    "id"=>2,
                    "key"=>10011,
                    "title"=> "自我领导力",
                    "type"=>"user",
                    "url"=>"/book",
                    "children"=>[
                        [
                            "id"=>14,
                            "key"=>20044,
                            "title"=> "日签卡管理",
                            "type"=>"user",
                            "url"=>"/book/datecard"
                        ]
                    ]
                ]
            ]
        ];*/
        return json($res);
    }

    /***
     * 清楚缓存
     */
    public function clearcache(){

        $CACHE_PATH = Env::get('root_path').config('cache_pc').'cache';
        $TEMP_PATH = Env::get('root_path').config('cache_pc').'temp';
        $LOG_PATH = Env::get('root_path').config('cache_pc').'log';

        $CACHE_PATH2 = Env::get('root_path').config('cache_this').'cache';
        $TEMP_PATH2 = Env::get('root_path').config('cache_this').'temp';
        $LOG_PATH2 = Env::get('root_path').config('cache_this').'log';

        if (delete_dir_file($CACHE_PATH) && delete_dir_file($TEMP_PATH) && delete_dir_file($LOG_PATH)) {
            delete_dir_file($CACHE_PATH2);
            delete_dir_file($TEMP_PATH2);
            delete_dir_file($LOG_PATH2);
            $this->res['code']=0;
            $this->res['msg']='清除缓存成功!';
        } else {
            $this->res['code']=0;
            $this->res['msg']='清除缓存失败!';
        }
        Cache::clear();
        return json($this->res);
    }

    public function bindmenu(){
        $inout = input("post.");
        $url=$inout["url"];
        $arr = array_values(array_filter(explode("/",$inout["url"])));
        if(count($arr)>2 && $arr[count($arr)-1]=="list"){
            array_pop($arr);
            $url="/".implode("/", $arr);
        }
        $cate=Rls::select();
        $rls=Rls::where('url',$url)->find();
        $pids = getCateId($cate,$rls['id']);
        $this->res=[
            'code'=>0,
            'data'=>[
                'key'=>$rls['id'],
                'openkeys'=>$pids
            ]
        ];
        return json($this->res);
    }

    //上传
    public function upload(){
        $file = request()->file('file');
        $type = input('post.type');
        $size = input('post.size');
        $name = input('post.path') ? input('post.path') : 'default';

        $filename1=Env::get('root_path') .config('upload_file'). 'public' . DIRECTORY_SEPARATOR . 'uploads'.DIRECTORY_SEPARATOR;
        $filename2=$filename1.$name;
        $time=date("Ymd",request()->time() );
        $hash=date("YmdHis",request()->time() ).rand(100000,999999);
        if($type=='photo'){
            $info = $file->rule('uniqid')->validate(['size'=>1024*1024*$size,'ext'=>'jpeg,jpg,png,gif'])->move($filename2.DIRECTORY_SEPARATOR.$time,$hash);
        }else{
            $info = $file->rule('uniqid')->validate(['size'=>1024*1024*$size])->move($filename2.DIRECTORY_SEPARATOR.$time,$hash);
        }
        $this->res=[
            'code'=>3,
            'msg'=>'上传失败!'
        ];
        if($info){
            $names=$info->getFilename();

            $this->res['code']=0;
            $this->res['msg']='上传成功';
            //$this->res['imgs']=$names;
            $this->res['url']=DIRECTORY_SEPARATOR.'uploads'.DIRECTORY_SEPARATOR.$name.DIRECTORY_SEPARATOR.$time.DIRECTORY_SEPARATOR.$names;
        }else{
            $this->res['msg']=$file->getError();
        }
        return json($this->res);
    }

    //规则数
    public function ruletree(){
        $get = input('get.');
        $rule = \app\common\model\Rule::select();
        $arrid=[];
        if($get['id']!=0){
            $arrid=getChildsId($rule,$get['id']);
            array_push($arrid,$get['id']);
        }
        $data=data_tree($rule,'pid',$get['pid'],$arrid);
        array_unshift($data,['key'=>0,'title'=>'作为顶级规则','value'=>0]);
        $d=[
            "code"=>0,
        ];
        $d['data']=$data;
        return json($d);
    }
}